package ipms.necfgsyn;

import ipms.appbase.ClsAppBase;
import ipms.appbase.ClsDataBase;
import ipms.appbase.ClsPublic;

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author spdjnh
 * 2012.12.24
 * IPRAN网管系统--基本配置程序入口
 * 
 */
public class CfgSynServer extends ClsAppBase {

	private static Logger log = LoggerFactory.getLogger(CfgSynServer.class);
	private String sRmiAddr="127.0.0.1";
	private int iRmiPort=21099;
	
	public static void main(String[] args) {
		new CfgSynServer();
	}
	
	public CfgSynServer() {
		AppRunTime.App = this;
		setAppName("BasicCfgSyn");
		setAppVersion("Version: 1.0.0/Build id:201212241012");
		
		//初始化和从数据库装载数据成功后，程序开始工作
		if(iniApp() && loadData()) {
			
			try {
				//启动RMI注册服务
				LocateRegistry.createRegistry(iRmiPort);
				CfgSynImpl cfgsyn = new CfgSynImpl();
				if(cfgsyn.iniParam() && cfgsyn.loadNeInfo()) {
					Naming.rebind("//"+sRmiAddr+":"+String.valueOf(iRmiPort)+"/CfgSyn",cfgsyn);
					log.info("BasicCfgSyn server is ready.");
				} else {
					System.exit(0);
				}
				
			} catch (Exception e) {
				log.error("BasicCfgSyn()",e);
				System.exit(0);
			}  
		
		}else {
			log.info("Application failed to initialize!" );
		}
		
	}

	@Override
	protected boolean iniApp() {
		boolean b=true;
		
		try {
			Properties prop = ClsPublic.getPropXml("appini.xml");
			
			ClsDataBase.sDbDrv = prop.getProperty("DbDrv");
			ClsDataBase.sDbUrl = prop.getProperty("DbUrl");
			ClsDataBase.sDbUsr = prop.getProperty("DbUsr");
			ClsDataBase.sDbPwd = prop.getProperty("DbPwd");
			sRmiAddr = prop.getProperty("RmiAddr").trim();
			String s = prop.getProperty("RmiPort").trim();
			iRmiPort = Integer.parseInt(s);
			
			AppRunTime.MyProp = prop;
			
			log.info("Initialize app parameter success.");
	        
		} catch (Exception e) {
			log.error("iniApp()",e);
			b = false;
		}
		
		return b;
	}
	
	
	@Override
	/**
	 * 目前不需要装载数据
	 */
	public boolean loadData() {
		boolean b=true;
	
		
		return b;
	}
	
	@Override
	public void closeApp() {
		log.info("Application is closed.");
		System.exit(0);
	}
}
